package pass;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CreateXML {
	private Document document;
	private String filename;

	/**
	 * 构造方法产生文件 CreateXML myxml = new CreateXML("d:\\2.txt");
	 * 
	 * @param String
	 *            绝对路径
	 * @author:edior
	 * @date:Apr 29, 2009
	 * @version:v1.1
	 */
	public CreateXML(String path) {
		filename = path;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			document = builder.newDocument();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 创建根元素
	 */
	public Element createRoot(String name) {
		Element root = document.createElement(name);
		document.appendChild(root);
		return root;
	}

	/**
	 * 创建元素
	 * 
	 * @param Element
	 *            e父元素,String eName新元素名称,String textNode,String name新元素属性,String value新元素属性值
	 * @author:edior
	 * @date:Apr 29, 2009
	 * @version:v1.1
	 */
	public Element createElement(Element e, String eName, String textNode,
			String name, String value) {
		Element e2 = document.createElement(eName);
		if (!"".equals(name)) {
			if (value == null) {
				value = "";
			}
			e2.setAttribute(name, value);
		}
		//e2.appendChild(document.createTextNode(textNode));
		e2.appendChild(document.createCDATASection(textNode));
		e.appendChild(e2);
		return e2;
	}

	/**
	 * 向文件写入
	 * 
	 * @param String
	 *            虚拟表； List 虚拟表字段； String 所有实体表名； List 实体表列表
	 * @author:edior
	 * @date:Apr 29, 2009
	 * @version:v1.1
	 */
	public void toWrite(Element root, String tableName, String idName, HashMap m) {
		Element t = document.createElement("table");
		t.setAttribute("tableName", tableName);
		t.setAttribute("idName", idName);
		root.appendChild(t);
		List list1 = (List) m.get(0);
		List aList = new ArrayList();
		for (int i2 = 1; i2 < m.size(); i2++) {
			aList = (List) m.get(i2);
			Element row = document.createElement("row");
			t.appendChild(row);
			/*
			 * for (int i = 0; i < list1.size(); i++) {// 字段个数 Element f1 =
			 * document.createElement((String) list1.get(i)); //
			 * f1.setAttribute("field",(String) list1.get(i)); if (((String)
			 * aList.get(i)) == null) {// 怎么处理？
			 * f1.appendChild(document.createTextNode("")); } else {
			 * f1.appendChild(document.createTextNode((String) aList .get(i))); }
			 * row.appendChild(f1); }
			 */
		}

	}

	/**
	 * 保存文件编码信息
	 * 
	 * @param
	 * @author:edior
	 * @date:Apr 29, 2009
	 * @version:v1.1
	 */
	public void toSave() {
		try {
			OutputStream fileoutputStream = new FileOutputStream(filename);
		    TransformerFactory tFactory = TransformerFactory.newInstance();
		    Transformer transformer = tFactory.newTransformer();
		    DOMSource source = new DOMSource(document);
		    StreamResult result = new StreamResult(new OutputStreamWriter(fileoutputStream,"utf-8"));
		    transformer.transform(source, result);

			/*TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			DOMSource source = new DOMSource(document);
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			PrintWriter pw = new PrintWriter(new FileOutputStream(filename
					+ ".xml"));
			StreamResult result = new StreamResult(pw);
			transformer.transform(source, result);
			pw.close();*/
		} catch (TransformerException mye) {
			mye.printStackTrace();
		} catch (IOException exp) {
			exp.printStackTrace();
		}

		/*try {
			File f = new File(filename + ".xml");
			FileInputStream in = new FileInputStream(f);
			
			java.io.BufferedReader breader = new BufferedReader(
					new InputStreamReader(in));

			java.io.BufferedWriter outw = null;
			java.io.FileOutputStream writerStream = new java.io.FileOutputStream(
					filename);
			outw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
					writerStream, "UTF-8"));
			String currentLine;

			while ((currentLine = breader.readLine()) != null) {
				currentLine = currentLine + "\r\n";
				outw.write(currentLine);
			}

			outw.flush();
			outw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}

	/*
	 * public static void main(String args[]) { try { List list = new
	 * ArrayList(); list.add("article.title;tstj.bt");
	 * list.add("article.content;tstj.nr"); list.add("article.time;tstj.time");
	 * CreateXMLtest myxml = new CreateXMLtest("d:\\2.txt");
	 * myxml.toWrite("test", list, "3", list); myxml.toSave();
	 * System.out.print("Your writing is successful."); } catch
	 * (ParserConfigurationException exp) { exp.printStackTrace();
	 * System.out.print("Your writing is failed."); } }
	 */
}